﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProgNetComponentsDx.Browsers;
using ProgNetComponents.Classes;
using ProgNetComponents.Data.SqlCe;
using ProgNetComponentsDx.Forms;

namespace BuildHelper.Work.Template
{
    public partial class BaseTemplateForm : MyBaseSqlCeForm
    {
        public BaseTemplateForm()
        {
            InitializeComponent();
        }
        public BaseTemplateForm(string template):base(template)
        {
            InitializeComponent();
        }
        
        protected override void OnLoad()
        {
            selectDataProvider1.GetSettings(this).Parameters["GRUPA"].Value = GetGrupa();
            base.OnLoad();
            if (!this.IsDesignMode())
            {
                templateControl1.BrowserId = this.BrowserId;
            }
        }
        public DataSet TemplateDataSet
        {
            get;
            set;
        }
        protected override void FillParams()
        {
            selectDataProvider1.GetSettings(this).Parameters["NAZWA"].Value = CurrentRecord;
        }

        protected override bool GetRecord()
        {
            bool result = base.GetRecord();
            if (result)
            {
                ReadTemplate();
                changeDetectorProvider1.ResetAll();
            }
            return result;
        }
        protected override bool ValidateAll()
        {
            if (modeProvider1.CurrentMode == ProgNetComponentsDx.Enums.FormMode.New)
            {
                return !BrowserCache.IsValid(BrowserId, false, CurrentRecord);
            }
            return true;
        }
        protected override void FinishEdit()
        {
            base.FinishEdit();
            string s = TemplateDataSet.WriteXmlString();
            if (MainRow != null)
                MainRow["STP_TMP_XML"] = s.AsByteArray();
            CurrentRecord = textEdit1.EditValue.AsString();
        }
        
        protected virtual void ReadTemplate()
        {
            try
            {
                if (MainRow != null)
                {
                    TemplateDataSet.Clear();
                    TemplateDataSet.ReadXmlString(((byte[])(MainRow["STP_TMP_XML"])).ByteArrayToString());
                    TemplateDataSet.AcceptChanges();
                }
            }
            catch { }
        }
        protected string GetGrupa()
        {
            Browser b = BrowserCache.GetBrowser(this.BrowserId, false);
            if (b != null)
            {
                string grupa = b.Filter.GetFirstMatch("STP_TMP_GRUPA\\s*=\\s*'(.*)'");
                return grupa;
            }
            return "";
        }
        protected override bool NewRecord()
        {
            TemplateDataSet.Clear();
            bool res = base.NewRecord();
            if (MainRow != null)
            {
                MainRow["STP_TMP_GRUPA"] = GetGrupa();
            }
            return res;
        }

        private void templateControl1_ReadingTemplate(object sender, MergingDataSetEventArgs e)
        {
            changeDetectorProvider1.SetModified();
        }
    }
}
